package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.starlink.common.domain.BaseEntity;
import com.starlink.mapper.MallBooksMapper;
import com.starlink.mapper.SchoolbagBooksStorageMapper;
import com.starlink.model.domain.MallBooksDO;
import com.starlink.model.domain.SchoolbagBooksStorageDO;
import com.starlink.model.domain.SchoolbagGroupBagBooksDO;
import com.starlink.model.request.SchoolbagBooksBatchRequest;
import com.starlink.service.SchoolbagBooksStorageService;
import com.starlink.service.SchoolbagGroupBagBooksService;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.SchoolbagBooksDtoMapper;
import com.starlink.model.domain.SchoolbagBooksDO;
import com.starlink.mapper.SchoolbagBooksMapper;
import com.starlink.model.request.SchoolbagBooksRequest;
import com.starlink.service.SchoolbagBooksService;
import com.starlink.model.util.SchoolbagBooksConvert;
import com.starlink.model.vo.SchoolbagBooksVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;
import java.util.Objects;

/**
 * @author zhanghong
 */
@Service
public class SchoolbagBooksServiceImpl extends ServiceImpl<SchoolbagBooksMapper, SchoolbagBooksDO> implements SchoolbagBooksService {
    @Autowired
    private SchoolbagBooksMapper schoolbagBooksMapper;
    @Autowired
    private MallBooksMapper mallBooksMapper;
    @Autowired
    private SchoolbagBooksStorageMapper schoolbagBooksStorageMapper;
    @Autowired
    private SchoolbagBooksStorageService schoolbagBooksStorageService;
    @Autowired
    private SchoolbagGroupBagBooksService schoolbagGroupBagBooksService;

    @Override
    public SchoolbagBooksVO getByKey(long id) {
        SchoolbagBooksVO schoolbagBooksVO = SchoolbagBooksDtoMapper.INSTANCE.do2VO(getById(id));
        assembleInfo(schoolbagBooksVO);
        return schoolbagBooksVO;
    }

    @Override
    public PageInfo<SchoolbagBooksVO> pageQuery(Pageable pageable,SchoolbagBooksRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<SchoolbagBooksDO> list = schoolbagBooksMapper.listQuery(request);
        List<SchoolbagBooksVO> schoolbagBooksVOList = SchoolbagBooksConvert.buildSchoolbagBooksVoList(list);
        for (SchoolbagBooksVO item : schoolbagBooksVOList) {
            assembleInfo(item);
        }
        return new PageInfo(schoolbagBooksVOList);
    }

    @Override
    public List<SchoolbagBooksVO> listQuery(SchoolbagBooksRequest request) {
        List<SchoolbagBooksDO> list = schoolbagBooksMapper.listQuery(request);
        List<SchoolbagBooksVO> schoolbagBooksVOS = SchoolbagBooksConvert.buildSchoolbagBooksVoList(list);
        for (SchoolbagBooksVO item : schoolbagBooksVOS) {
            assembleInfo(item);
        }
        return schoolbagBooksVOS;
    }

    private void assembleInfo(SchoolbagBooksVO item) {
        // 书籍信息
        MallBooksDO mallBooksDO = mallBooksMapper.selectDOById(item.getMallBooksId());
        if (mallBooksDO != null) {
            item.setTitle(mallBooksDO.getTitle());
            item.setPicture(mallBooksDO.getPicture());
            item.setAuthor(mallBooksDO.getAuthor());
            item.setPublishingHouse(mallBooksDO.getPublishingHouse());
        }
        // 库存信息
        Integer storage = schoolbagBooksStorageMapper.countByBooksId(item.getId());
        item.setStorage(storage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(SchoolbagBooksRequest request) {
        AssertUtil.assertNotNull(request.getMallBooksId(), "商城书籍id不能为空");
        MallBooksDO mallBooksDO = mallBooksMapper.selectDOById(request.getMallBooksId());
        AssertUtil.assertNotNull(mallBooksDO, "商城书籍不存在");

        Long count = this.lambdaQuery().eq(SchoolbagBooksDO::getMallBooksId, request.getMallBooksId()).count();
        AssertUtil.assertTrue(count == 0, "商城书籍已存在");
        save(SchoolbagBooksDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(SchoolbagBooksRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(SchoolbagBooksDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        // 书籍有库存存在，则不能被删除
        for (String id : Convert.toStrArray(ids)) {
            Long count = schoolbagBooksStorageService.lambdaQuery().eq(SchoolbagBooksStorageDO::getBooksId, id).eq(BaseEntity::getDeleted, 0).count();
            AssertUtil.assertTrue(Objects.equals(count, 0L), "书籍存在库存不能被删除");
        }
        // 书籍正在被书包引用，则不能删除
        for (String id : Convert.toStrArray(ids)) {
            Long count = schoolbagGroupBagBooksService.lambdaQuery().eq(SchoolbagGroupBagBooksDO::getBookId, id).eq(BaseEntity::getDeleted, 0).count();
            AssertUtil.assertTrue(Objects.equals(count, 0L), "书籍正在被书包引用不能被删除");
        }
        schoolbagBooksMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    public Boolean batchAdd(SchoolbagBooksBatchRequest request) {
        AssertUtil.assertNotEmpty(request.getMallBooksIdList(), "商城书籍id不能为空");
        for (Long mallBooksId : request.getMallBooksIdList()) {
            MallBooksDO mallBooksDO = mallBooksMapper.selectDOById(mallBooksId);
            AssertUtil.assertNotNull(mallBooksDO, "商城书籍不存在");

            Long count = this.lambdaQuery().eq(SchoolbagBooksDO::getMallBooksId, mallBooksId).count();
            AssertUtil.assertTrue(count == 0, "商城书籍已存在");
            save(SchoolbagBooksDO.builder().mallBooksId(mallBooksId).build());
        }
        return true;
    }
}
